package com.data.modules.smzh.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.data.common.api.vo.Result;
import com.data.common.util.JwtIOTAuthUtil;
import com.data.modules.smzh.entity.OftenContact;
import com.data.modules.smzh.entity.SmComAnnouncement;
import com.data.modules.smzh.mapper.FacedeviceMapper;
import com.data.modules.smzh.mapper.SmComAnnouncementMapper;
import com.data.modules.smzh.service.ISmComAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 小区公告信息
 * @Author: data-boot
 * @Date:   2023-01-04
 * @Version: V1.0
 */
@Service
public class SmComAnnouncementServiceImpl extends ServiceImpl<SmComAnnouncementMapper, SmComAnnouncement> implements ISmComAnnouncementService {

    @Autowired
    private FacedeviceMapper facedeviceMapper;

    @Override
    public Result<?> smAddInfo(SmComAnnouncement smComAnnouncement, HttpServletRequest req) {

        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);

            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                String userName = JwtIOTAuthUtil.getUserNameByRequest(req);
                reaAppId = facedeviceMapper.queryDevelAppId(appId);

                if(ObjectUtil.contains(smComAnnouncement.getComNo(),",")){

                    String finalReaAppId = reaAppId;
                    Arrays.asList(smComAnnouncement.getComNo().split(",")).stream().forEach(
                            x->{

                                SmComAnnouncement sm = new SmComAnnouncement();

                                sm.setCreateBy(userName);
                                sm.setAppId(finalReaAppId);
                                sm.setComNo(x);
                                sm.setImage(smComAnnouncement.getImage());
                                this.save(sm);
                            }
                    );

                }else{
                        smComAnnouncement.setCreateBy(userName);
                        smComAnnouncement.setAppId(reaAppId);
                        this.save(smComAnnouncement);

                }
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,"操作成功");

    }

    @Override
    public Result<?> searchAddInfo(SmComAnnouncement smComAnnouncement, HttpServletRequest req) {

        List<SmComAnnouncement> list = new ArrayList<>();
        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceMapper.queryDevelAppId(appId);

                list = this.lambdaQuery().eq(SmComAnnouncement::getAppId,reaAppId)
                        .eq(ObjectUtil.isNotEmpty(smComAnnouncement.getComNo()),SmComAnnouncement::getComNo,smComAnnouncement.getComNo())
                        .list();
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,list);
    }

    @Override
    public Result<?> smDeleteAdd(SmComAnnouncement smComAnnouncement, HttpServletRequest req) {

        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceMapper.queryDevelAppId(appId);

                this.removeById(smComAnnouncement.getId());
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }
        return Result.ok(0,"操作成功");
    }

    @Override
    public Result<?> smQueryAddInfo(SmComAnnouncement smComAnnouncement, HttpServletRequest req) {

        try {
            String appId = JwtIOTAuthUtil.getTokenByRequest(req);
            String reaAppId = "";
            if(ObjectUtil.isEmpty(appId)){
                return Result.error(10001,"无效token");
            }else{
                reaAppId = facedeviceMapper.queryDevelAppId(appId);

                SmComAnnouncement vo = this.getById(smComAnnouncement.getId());
                return Result.ok(0,vo);
            }

        } catch (Exception e) {
            return Result.error(10001,e.getMessage());
        }

    }
}
